package com.logic.landseaserver.models.payment.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.exception.ResultCodes.PaymentResultCode;
import com.logic.landseaserver.domain.BillPayment;
import com.logic.landseaserver.domain.RealBill;
import com.logic.landseaserver.models.independent.service.BillIndependentService;
import com.logic.landseaserver.models.payment.PaymentConstant;
import com.logic.landseaserver.models.payment.PaymentConstant.BillTypeCode;
import com.logic.landseaserver.models.payment.bean.PaymentHelperBean;
import com.logic.landseaserver.models.payment.service.AbstractPaymentResponseHandler;
import com.logic.landseaserver.models.prepayment.bean.AotuPayBill;
import com.logic.landseaserver.persistence.read.RealBillReadMapper;
import com.logic.landseaserver.persistence.write.RealBillWriteMapper;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：RealBillResponseHandler</br>
 * 类描述：实时账单结果通知处理</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月19日 下午4:33:21</br>
 * 
 * @version 1.0
 *
 */
@Service(BillTypeCode.REAL_TIME + PaymentConstant.PAYMENT_RESPONSE_SUFFIX)
public class RealBillResponseHandler extends AbstractPaymentResponseHandler
{
    @Autowired
    private RealBillWriteMapper realBillWriteMapper;
    
    @Autowired
    private RealBillReadMapper realBillReadMapper;
    
    @Autowired
    private BillIndependentService billIndependentService;
    
    @Override
    protected void doLocalBusiness(BillPayment payment, PaymentHelperBean helperObject, Map<String, Object> resultMap)
        throws LandseaException
    {
        String logInfo = getLog(payment, payment.getId());
        LOGGER.info(logInfo + "updateBillStateAndMoney finished.");

        try
        {
            // 实时账单
            RealBill realBill = realBillReadMapper.selectByPrimaryKey(payment.getBillId());
            if (null == realBill)
            {
                LOGGER.error(logInfo + "no record in ls_real_bill table with billId=" + payment.getBillId());
                throw LandseaException.createException(PaymentResultCode.E00150002);
            }
            
            realBillWriteMapper.updateStatById(realBill);      
            //银石支付修改balanceSheet中的状态
            billIndependentService.realUpdateBalanceSheet(payment.getBillId(), payment.getTime());
            
            LOGGER.info(logInfo + "update real bill state and billNo finished.");
        }
        catch (LandseaException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            LOGGER.error(logInfo + "update real bill state and billNo occur a error.", e);
            throw LandseaException.createException(PaymentResultCode.E00150018);
        }
    }
    
    @Override
    protected String getProcessClassName()
    {
        return this.getClass().getSimpleName();
    }

    /**    
     * [描述]：</br>
     *
     * @param aotuPayBill
     * @param billPayment
     * @throws LandseaException
     *
    */
    @Override
    public void aotoPayDoLocalBusiness(AotuPayBill aotuPayBill, BillPayment payment)
        throws LandseaException
    {
        String logInfo = "余额自动抵扣实时账单|";
        LOGGER.info(logInfo + "处理开始.");

        try
        {
            // 实时账单
            RealBill realBill = realBillReadMapper.selectByPrimaryKey(payment.getBillId());
            if (null == realBill)
            {
                LOGGER.error(logInfo + "实时账单记录不存在,billId=" + payment.getBillId());
                throw LandseaException.createException(PaymentResultCode.E00150002);
            }
            realBillWriteMapper.updateStatById(realBill);
            //银石支付修改balanceSheet中的状态
            billIndependentService.realUpdateBalanceSheet(payment.getBillId(), payment.getTime());
            
            LOGGER.info(logInfo + "处理实时账单结束.");
        }
        catch (LandseaException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            LOGGER.error(logInfo + "更新实时账单发生错误.", e);
            throw LandseaException.createException(PaymentResultCode.E00150018);
        }        
    }
}
